#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int f[200000][3];
struct t{int a,b;}k[200000];
signed main()
{
    freopen("boast.in","r",stdin);
    freopen("boast.out","w",stdout);
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
    scanf("%lld%lld",&k[i].a,&k[i].b);
    sort(k+1,k+n+1,[](t x,t y){return x.b>y.b;});
    f[0][0]=f[0][1]=f[0][2]=LONG_LONG_MAX/2;
    for(int i=1;i<=n;i++)
    {
        f[i][0]=min(f[i-1][0],k[i].a+k[i].b);
        f[i][1]=min(f[i-1][1],f[i-1][0]+k[i].a+k[i].b);
        f[i][2]=min(f[i-1][2],f[i-1][1]+k[i].a);
    }
    printf("%lld",f[n][2]);
    return 0;
}